package ldh.msg.ui.helper;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MyNetworkPanel extends JPanel {
	private static final long serialVersionUID = 50155015L;
	private JCheckBox useChkBx = null;
	private IntegerTextField ipATF = null;
	private IntegerTextField ipBTF = null;
	private IntegerTextField ipCTF = null;
	
	public MyNetworkPanel() {
		super();
		FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
		layout.setHgap(0);
		layout.setVgap(0);
		setLayout(layout);
		
		setPreferredSize(new Dimension(220, 24));
		setMinimumSize(new Dimension(220, 24));
		setMaximumSize(new Dimension(220, 24));
		useChkBx = new JCheckBox();
		useChkBx.setSelected(false);
		ipATF = new IntegerTextField(3);
		ipATF.setDocument(new JTextFieldLimit(3, 255));
		ipATF.setPreferredSize(new Dimension(20, 22));
		ipATF.setMaximumSize(new Dimension(20, 22));
		ipBTF = new IntegerTextField(3);
		ipBTF.setDocument(new JTextFieldLimit(3, 255));
		ipBTF.setPreferredSize(new Dimension(20, 22));
		ipBTF.setMaximumSize(new Dimension(20, 22));
		ipCTF = new IntegerTextField(3);
		ipCTF.setDocument(new JTextFieldLimit(3, 255));
		ipCTF.setPreferredSize(new Dimension(20, 22));
		ipCTF.setMaximumSize(new Dimension(20, 22));
		add(useChkBx);
		add(ipATF);
		add(new JLabel("."));
		add(ipBTF);
		add(new JLabel("."));
		add(ipCTF);
		add(new JLabel(".255"));
	}

	public boolean isUseChkBx() {
		return useChkBx.isSelected();
	}

	public void setUseChkBx(boolean flag) {
		this.useChkBx.setSelected(flag);
	}

	public String getIpATF() {
		return ipATF.getText();
	}

	public void setIpATF(String ipATF) {
		this.ipATF.setText(ipATF);
	}

	public String getIpBTF() {
		return ipBTF.getText();
	}

	public void setIpBTF(String ipBTF) {
		this.ipBTF.setText(ipBTF);
	}

	public String getIpCTF() {
		return ipCTF.getText();
	}

	public void setIpCTF(String ipCTF) {
		this.ipCTF.setText(ipCTF);
	}
	
	public String getIpList(String ipCTF) {
		return this.ipATF.getText() + "." + this.ipBTF.getText() + "." + this.ipCTF.getText();
	}
	
}

class IntegerTextField extends JTextField {
	private static final long serialVersionUID = 50505050L;
	final static String badchars 
       = "`~!@#$%^&*()_+=\\|\"':;?/>.<, ";
    public IntegerTextField(int col) {
    	super(col);
//    	setSize(new Dimension());
    }
    public void processKeyEvent(KeyEvent ev) {

        char c = ev.getKeyChar();

        if((Character.isLetter(c) && !ev.isAltDown()) 
           || badchars.indexOf(c) > -1) {
            ev.consume();
            return;
        }
        if(c == '-' && getDocument().getLength() > 0) 
        	ev.consume();
        else super.processKeyEvent(ev);

    }
}

